package cryptography.symmetric.TripleDES;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;


import static org.junit.Assert.assertEquals;

//Cần thư viện
//commons-codec-1.2.jar
//junit-4.1.jar
public class TripleDESCryptography2 {

//	private static final String PLAIN_TEXT = "Some-plain-text-message-to-be-symetrically-encrypted";
//
//    private static String CIPHER_TEXT = "someCipherText";
//
//    private static final String SHARED_KEY = "Some-Shared-Secret";
//	
//	public static void main(String[] args) throws Exception {
//
//		String algorithm = "DESede";
//        String transformation = "DESede/CBC/PKCS5Padding";
//
//        final MessageDigest md = MessageDigest.getInstance("md5");
//		final byte[] digestOfPassword = md.digest(Base64.decode(SHARED_KEY
//				.getBytes("utf-8")));
//		final byte[] keyValue = Arrays.copyOf(digestOfPassword, 24);
//		for (int j = 0, k = 16; j < 8;) {
//			keyValue[k++] = keyValue[j++];
//		}
//        
//        DESedeKeySpec keySpec = new DESedeKeySpec(keyValue);
//
//        /* Initialization Vector of 8 bytes set to zero. */
//        IvParameterSpec iv = new IvParameterSpec(new byte[8]);
//
//        SecretKey key = SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec);
//        Cipher encrypter = Cipher.getInstance(transformation);
//        encrypter.init(Cipher.ENCRYPT_MODE, key, iv);
//
//        byte[] input = PLAIN_TEXT.getBytes("UTF-8");
//
//        byte[] encrypted = encrypter.doFinal(input);
//
//        CIPHER_TEXT = new String(Base64.encode(encrypted), "UTF-8");
//        
//        Cipher decrypter = Cipher.getInstance(transformation);
//        decrypter.init(Cipher.DECRYPT_MODE, key, iv);
//        
//        byte[] decrypted = decrypter.doFinal(Base64.decode(CIPHER_TEXT.getBytes()));
//
//        assertEquals(PLAIN_TEXT, new String(decrypted, "UTF-8"));
//        
//        System.out.println("Plain Text : " + PLAIN_TEXT);
//		System.out.println("Encrypted : " + CIPHER_TEXT);
//		System.out.println("Decrypted : " + new String(decrypted, "UTF8"));
//		
//	}
}
